{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "ca9e1cd2-a0df-4d5c-b158-40a30ffc30e9",
   "metadata": {},
   "source": [
    "# LLM Predictor"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "92fae55a-6bf9-4d34-b831-6186afb83a62",
   "metadata": {
    "tags": []
   },
   "source": [
    "## LangChain LLM"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "0ec45369-c4f3-48af-861c-a2e1d231ad2f",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from langchain.chat_models import ChatOpenAI\n",
    "from llama_index import LLMPredictor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "d9561f78-f918-4f8e-aa3c-c5c774dd9e01",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "llm_predictor = LLMPredictor(ChatOpenAI())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "e52d30f9-2127-4975-9906-02c8a827ba74",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "stream = await llm_predictor.astream(\"Hi, write a short story\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "db714473-e38f-4ed6-adba-4a1f82fd3067",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Once upon a time in a small village nestled in the mountains, there lived a young girl named Lily. She was known for her curiosity and adventurous spirit. Lily spent her days exploring the vast forests surrounding her village, always searching for something new and exciting.\n",
      "\n",
      "One sunny morning, as Lily was venturing deeper into the woods, she stumbled upon a hidden path she had never seen before. Intrigued, she decided to follow it, hoping it would lead her to an unknown world full of wonders. The path wound its way through thick foliage, and after what seemed like hours, Lily emerged into a breathtaking clearing.\n",
      "\n",
      "In the center of the clearing stood an enormous oak tree, its branches reaching towards the sky. Lily's eyes widened with awe as she noticed a small door nestled within the trunk. Unable to resist her curiosity, she tentatively pushed the door, which creaked open to reveal a narrow staircase spiraling down into darkness.\n",
      "\n",
      "Without hesitation, Lily began her descent, her heart pounding with anticipation. As she reached the bottom of the stairs, she found herself in a magical underground chamber illuminated by soft, shimmering lights. The walls were adorned with intricate murals depicting mythical creatures and faraway lands.\n",
      "\n",
      "As Lily explored further, she stumbled upon a book lying on a pedestal. The cover was worn and weathered, but the pages were filled with enchanting tales of dragons, fairies, and brave knights. It was a book of forgotten stories, waiting to be rediscovered.\n",
      "\n",
      "With each page Lily turned, the stories came to life before her eyes. She found herself transported to distant lands, where she met extraordinary beings and embarked on incredible adventures. She befriended a mischievous fairy who guided her through a labyrinth, outsmarted a cunning dragon to rescue a kidnapped prince, and even sailed across treacherous seas to find a hidden treasure.\n",
      "\n",
      "Days turned into weeks, and Lily's love for exploration only grew stronger. The villagers began to notice her absence and wondered where she had disappeared to. Her family searched high and low, but Lily was nowhere to be found.\n",
      "\n",
      "One evening, as the sun began to set, Lily closed the book, her heart filled with gratitude for the magical journey it had taken her on. She knew it was time to return home and share her incredible adventures with her loved ones.\n",
      "\n",
      "As she emerged from the hidden chamber, Lily found herself standing in the same clearing she had discovered weeks earlier. She smiled, knowing she had experienced something extraordinary that would forever shape her spirit.\n",
      "\n",
      "Lily returned to her village, where she was greeted with open arms and wide smiles. She shared her stories with the villagers, igniting their imaginations and sparking a sense of wonder within them. From that day forward, Lily's village became a place of creativity and exploration, where dreams were nurtured and curiosity was celebrated.\n",
      "\n",
      "And so, the legend of Lily, the girl who discovered the hidden door to a world of enchantment, lived on for generations to come, inspiring countless others to seek their own hidden doors and embark on magical adventures of their own."
     ]
    }
   ],
   "source": [
    "for token in stream:\n",
    "    print(token, end=\"\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "57777376-08ef-4aeb-912c-3efdb1451c65",
   "metadata": {},
   "source": [
    "## OpenAI LLM"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "80a21a67-f992-401e-a0d9-53d411a4e8ba",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "from llama_index.llms import OpenAI\n",
    "from llama_index import LLMPredictor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "6cb1913e-febf-4c01-b2ad-634c007bd9aa",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "llm_predictor = LLMPredictor(OpenAI())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "e74b61a8-dff0-49f6-9004-dfe265f3f053",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "stream = await llm_predictor.astream(\"Hi, write a short story\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "9204e248-8a43-422c-b083-52b119c52642",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Once upon a time in a small village nestled in the heart of a lush forest, there lived a young girl named Lily. She was known for her kind heart and adventurous spirit. Lily spent most of her days exploring the woods, discovering hidden treasures and befriending the creatures that called the forest their home.\n",
      "\n",
      "One sunny morning, as Lily ventured deeper into the forest, she stumbled upon a peculiar sight. A tiny, injured bird lay on the ground, its wings trembling. Lily's heart filled with compassion, and she carefully picked up the bird, cradling it in her hands. She decided to take it home and nurse it back to health.\n",
      "\n",
      "Days turned into weeks, and the bird, whom Lily named Pip, grew stronger under her care. Pip's once dull feathers regained their vibrant colors, and his wings regained their strength. Lily knew it was time for Pip to return to the wild, where he truly belonged.\n",
      "\n",
      "With a heavy heart, Lily bid farewell to her feathered friend, watching as Pip soared into the sky, his wings carrying him higher and higher. As she stood there, a sense of emptiness washed over her. She missed Pip's cheerful chirping and the companionship they had shared.\n",
      "\n",
      "Determined to fill the void, Lily decided to embark on a new adventure. She set out to explore the forest in search of a new friend. Days turned into weeks, and Lily encountered various animals, but none seemed to be the perfect companion she longed for.\n",
      "\n",
      "One day, as she sat by a babbling brook, feeling disheartened, a rustling sound caught her attention. She turned around to find a small, fluffy creature with bright blue eyes staring back at her. It was a baby fox, lost and scared. Lily's heart melted, and she knew she had found her new friend.\n",
      "\n",
      "She named the fox Finn and took him under her wing, just as she had done with Pip. Together, they explored the forest, climbed trees, and played hide-and-seek. Finn brought joy and laughter back into Lily's life, and she cherished their bond.\n",
      "\n",
      "As the years passed, Lily and Finn grew older, but their friendship remained strong. They became inseparable, exploring the forest and facing its challenges together. Lily learned valuable lessons from the forest and its creatures, and she shared these stories with Finn, who listened intently.\n",
      "\n",
      "One day, as they sat beneath their favorite oak tree, Lily realized how much she had grown since she first found Pip. She had learned the importance of compassion, friendship, and the beauty of nature. The forest had become her sanctuary, and its creatures her family.\n",
      "\n",
      "Lily knew that her adventures would continue, and she would always find new friends along the way. With Finn by her side, she was ready to face any challenge that awaited her. And so, hand in paw, they set off into the forest, ready to create new memories and embark on countless adventures together."
     ]
    }
   ],
   "source": [
    "for token in stream:\n",
    "    print(token, end=\"\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
